<!-- user表单弹窗 -->
<script src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>
<form id="activity-form" lay-filter="activity-form" class="layui-form model-form">
    <input id="id" name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input name="title" placeholder="请填写活动标题" type="text" class="layui-input"/>
        </div>
    </div>
    <div class="layui-upload">
        <label class="layui-form-label">封面图片:</label>
        <div class="layui-upload layui-input-block">
            <input type="hidden" name="headerImg" />
            <button type="button" class="layui-btn layui-btn-primary" id="fileBtn"><i class="layui-icon">&#xe67c;</i>选择文件</button>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">活动描述</label>
        <div class="layui-input-block">
            <div id="wangEditor"></div>
            <textarea style="display: none" id="desc" placeholder="请输入活动描述信息" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="activity-form-submit" lay-submit>保存</button>
    </div>
</form>
<script>
    layui.use(['layer', 'admin', 'form','upload','config'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var upload=layui.upload;
        var config = layui.config;

        // 回显user数据
        var activity = admin.getTempData('t_activity');
        console.log(activity);
        if (activity) {
            form.val('activity-form', activity);

        }
        console.log("editor")
        const E = window.wangEditor
        const editor = new E('#wangEditor')
        const $desc = $('#desc')
        editor.config.uploadImgServer = config.base_server+'api-file/files-wangEditor'
        editor.config.uploadFileName = 'file'
        editor.config.uploadImgParams = {
            access_token: config.getToken().access_token
        }
        editor.config.onchange = function (html) {
            // 第二步，监控变化，同步更新到 textarea
            $desc.val(html)
        }
        editor.create()
        editor.txt.html(activity==null?'':activity.desc)


        //文件上传
        upload.render({
            elem: '#fileBtn' ,
            url:  config.base_server + 'api-file/files-anon',
            accept: 'file',
            auto: true ,
            before: function(obj){
                layer.load(); //上传loading
                this.data={access_token:config.getToken().access_token
                };
            },
            done: function(data){
                layer.closeAll('loading');
                // console.log(data);
                $("[name=headerImg]").val(data.url);
            },
            error: function (data) {
                layer.closeAll('loading');
            }
        });

        // 表单提交事件
        form.on('submit(activity-form-submit)', function (data) {
            layer.load(2);
            data.field.desc=$("#desc").val();
            if(data.field.id==null || data.field.id==""){
                var user=config.getUser();
                data.field.host=user;
                admin.req('api-act/activity', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'PUT');
                return false;
            }else {
                data.field.host=activity.host
                admin.req('api-act/activity', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'POST');
                return false;
            }

        });
    });
</script>